package com.huanletao.config;

import org.apache.commons.configuration2.*;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.tree.DefaultExpressionEngine;
import org.apache.commons.configuration2.tree.DefaultExpressionEngineSymbols;
import org.junit.Test;

import java.util.Iterator;

/**
 * <p>
 *   使用commons-configuration2 读取配置文件示例:
 *     目前只展示最常用的三种配置文件;
 *       properties , xml , yaml
 * </p>
 *
 * @package: com.huanletao.config
 * @description:
 * @className: ReadConfigurationsApiTest
 * @author: @踏雪无痕  ------
 * @date: Created in 2022/5/30
 * @copyright: Copyright (c) 2022/5/30
 * @version: V1.0
 */
public class ReadConfigurationsApiTest {

    /**
     * read Config from Yaml file
     *
     */
    @Test
    public void readYamlConfiguration() {

        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<YAMLConfiguration> builder =
                new FileBasedConfigurationBuilder<>(YAMLConfiguration.class)
                        .configure(params.fileBased()
                                .setFileName("configuration/config.yaml"));

        try {
            YAMLConfiguration config = builder.getConfiguration();
            // 使用默认的符号定义创建一个表达式引擎
            DefaultExpressionEngine engine = new DefaultExpressionEngine(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS);
            //指定表达式引擎
            config.setExpressionEngine(engine);
            Iterator<String> keys = config.getKeys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = config.getString(key);
                System.out.println(key + " = " + value);
            }
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }

    }

    /**
     * read config from properties
     * @throws ConfigurationException
     */
    @Test
    public void readPropertiesConfiguration(){

        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
                new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                        .configure(params.properties()
                         .setFileName("configuration/config.properties"));
        try
        {
            Configuration config = builder.getConfiguration();
            Iterator<String> keys = config.getKeys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = config.getString(key);
                System.out.println(key + " = " + value);
            }
        }
        catch(ConfigurationException cex)
        {
            // loading of the configuration file failed
        }

    }

    /**
     * read config from xml ;
     * @throws ConfigurationException
     */
    @Test
    public void readXmlConfiguration() throws ConfigurationException {

        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<XMLConfiguration> builder =
                new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
                        .configure(params.xml()
                         .setFileName("configuration/config.xml"));

        XMLConfiguration config = builder.getConfiguration();

        Iterator<String> keys = config.getKeys();
        while (keys.hasNext()) {
            String next = keys.next();
            String value = config.getString(next);
            System.out.println(next + " = " + value);
        }

       // 使用默认的符号定义创建一个表达式引擎
        DefaultExpressionEngine engine = new DefaultExpressionEngine(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS);
        //指定表达式引擎
        config.setExpressionEngine(engine);
    }
}
